<template>
        <div id="add_order" class="app">
            <div class="divHead">
                <div class="divTitle">
                    <i class="el-icon-arrow-left" @click="goBack"></i>校园送
                </div>
            </div>
            <div class="divContent">
                <div class="divAddress">
                    <div @click="toAddressPage">                    
                        <div class="address">{{address.detail}}</div>
                        <div class="name">
                            <span>{{address.consignee}}{{address.sex =='1' ? '先生':'女士'}}</span>
                            <span>{{address.phone}}</span>
                        </div>
                        <i class="el-icon-arrow-right"></i>
                    </div>
                    <div class="divSplit"></div>
                    <div class="divFinishTime">预计{{finishTime}}送达</div>
                </div>
                <div class="order">
                    <div class="title">订单明细</div>
                    <div class="divSplit"></div>
                    <div class="itemList">
                        <div class="item" v-for="(item,index) in cartData" :key="index">
                            <el-image :src="imgPathConvert(item.image)">
                                <div slot="error" class="image-slot">
                                    <img src="../assets/images/noImg.png"/>
                                </div>
                            </el-image>
                            <div class="desc">
                                <div class="name">{{item.name}}</div>
                                <div class="numPrice">
                                    <span class="num">x{{item.number}}</span>
                                    <div class="price">
                                        <span class="spanMoney">￥</span>{{item.amount}}</div>
                                    </div>
                                </div>
                            </div>
                    </div>
                </div>
                <div class="note">
                    <div class="title">备注</div>
                    <van-field
                        v-model="note"
                        rows="2"
                        autosize
                        type="textarea"
                        maxlength="50"
                        placeholder="请输入您需要备注的信息"
                        show-word-limit
                    />
                </div>
            </div>
            <div class="divCart">
                <div :class="{imgCartActive: cartData && cartData.length > 0, imgCart:!cartData || cartData.length<1}"></div>
                <div :class="{divGoodsNum:1===1, moreGoods:cartData && cartData.length > 99}" v-if="cartData && cartData.length > 0">{{ goodsNum }}</div>
                <div class="divNum">
                    <span>￥</span>
                    <span>{{goodsPrice+3}}</span>
                </div>
                <div class="divPrice"></div>
                <div :class="{btnSubmitActive: cartData && cartData.length > 0, btnSubmit:!cartData || cartData.length<1}" @click="goToPaySuccess">去支付</div>
              </div>
              
        </div>
       
</template>

<script>
import {getDefaultAddressApi} from '../assets/api/address'
import {cartListApi}from '../assets/api/main'
import {addOrderApi}from '../assets/api/order'
import{imgPath} from '../assets/js/common'
import {base} from '../assets/js/base'
import router from '@/router'
import axios from 'axios'
export default {
    name:"add_order",
    data(){
        return {
            ConfirmOrderobj:'',
            address:{
                phone:'',//手机号
                consignee:'',//姓名
                detail:'',//详细地址
                sex:'1',
                id:undefined
            },
            finishTime:'',//送达时间
            cartData:[],
            note:''//备注信息
        }
    },
    computed:{
        goodsNum(){
            let num = 0
            this.cartData.forEach(item=>{
                num += item.number 
            })
            if(num <99){
                return num
            }else{
                return '99+'
            }
        },
        goodsPrice(){
        let price = 0
        this.cartData.forEach(item=>{
            price += (item.number * item.amount)
        })
        return price
        }
    },
    created(){
        base(document,window)
        this.initData()
    },
    mounted(){},
    methods:{
        goBack(){
            history.go(-1)
        },
        initData(){
            //获取默认的地址
            this.defaultAddress()
            //获取购物车的商品
            this.getCartData()
        },
        //获取默认地址
        async defaultAddress(){
            const res = await  getDefaultAddressApi()
            if(res.data.code === 1){
                console.log(res.data.data);
                this.address = res.data.data
                this.getFinishTime()
            }else{
                window.requestAnimationFrame(()=>{
                    router.push('/address/edit')
                })
            }
        },
        //获取送达时间
        getFinishTime(){
            let now = new Date()
            let hour = now.getHours() +1
            let minute = now.getMinutes()
            if(hour.toString().length <2){
                hour = '0' + hour
            }
            if(minute.toString().length <2){
                minute = '0' + minute
            }
            this.finishTime = hour + ':' + minute
        },
        toAddressPage(){
            window.requestAnimationFrame(()=>{
                router.push('/address')
            })
        },
        //获取购物车数据
        async getCartData(){
            const res = await cartListApi({})
            if(res.data.code === 1){
                this.cartData = res.data.data
            }else{
                this.$notify({ type:'warning', message:res.data.msg});
            }
        },
        async goToPaySuccess(){
            const params = {
                remark:this.note,
                payMethod:1,
                addressBookId:this.address.id
            }
            const res = await addOrderApi(params)
            if(res.data.code === 1){
                // router.push("/paySuccess")
                window.location.href="http://127.0.0.1:6789/alipay/pay?subject="+res.data.data.subjectName+"&traceNo="+res.data.data.orderId+"&totalAmount="+res.data.data.totalAmount
                //  this.$router.push({path: '/aliPay', query: {traceNo: res.data.data.orderId,subject: res.data.data.subjectName,totalAmount: res.data.data.totalAmount }});
                
            }else{
                this.$notify({ type:'warning', message:res.msg});
            }
        },
        //网络图片路径转换
        imgPathConvert(path){
            return imgPath(path)
        },
    }

}
</script>
<style>
    @import url(../assets/styles/index.css);
    @import url(../assets/styles/add-order.css);
    @import url(../assets/styles/vant.min.css);
</style>